#include "trainmanagement.h"
//#include "train_mysql.h"
#include "app_mysql.h"
#include <iostream>

using namespace std;

Train TrainManagement::train;

void TrainManagement::deleteStation(int index)
{
    train.deleteStation(index);
}

void TrainManagement::addStation(Station station, QTime time, int index)
{
    train.insertStation(station, time, index);
}

void TrainManagement::deleteTrain()
{
    //call delete method
        database_start("root", "");
        char *ch;
        ch = train.ID.toLocal8Bit().data();
        deleteTrain_mysql(ch);
        database_end();

}

void TrainManagement::queryTrain(QString TrainNum)
{
    //call query method and train=result
    database_start("root", "");
    char *ch;
    ch = TrainNum.toLocal8Bit().data();
    Train tr;
    train = queryTrain_mysql(ch);

//    int len =tr.length();
//    if (len != 0) {
//        cout << len <<endl;
//        for (int i = 0; i<len; i++){
//            Station s;
//            QTime t;
//            tr.getItem(s, t, i);

//            cout << s.ID << s.name.toLocal8Bit().data() << t.toString().toLocal8Bit().data() <<endl;

////            s.name = QString(queryStation_mysql(s.ID));
////            train.appendStation(s,t);
//        }
//    }
//    train.ID = tr.ID;

    database_end();

//    train = tr;

    cout << train.ID.toLocal8Bit().data() << " " << train.length() << endl;
}

void TrainManagement::modifyTrain()
{
    //call modify method update var "train" into db

    database_start("root","");
    char *num = train.ID.toLocal8Bit().data();

    deleteTrain_mysql(num);

    int len = train.length();
    for (int i = 0; i < len; i++) {
        Station s;
        QTime t;
        train.getItem(s,t,i);

        addTrain_mysql(train.ID.toLocal8Bit().data(), s.ID, t.toString().toLocal8Bit().data());
    }

    database_end();
}

void TrainManagement::addTrain(QString trainNum)
{
    train.ID=trainNum;
}

QString* TrainManagement::getTrainList()
{
    if(train.ID == NULL)
        return NULL;

    int length = train.length();
    QString* res = new QString[length * 2 + 1];

//    cout << length << endl << res->length() << endl;

    res[0] = QString::number(length);

    for(int i = 0; i < length ; i++)
    {
        Station s;
        QTime t;
        train.getItem(s, t, i);
        res[i * 2 + 1] = s.name;
        res[i * 2 + 2] = t.toString("hh:mm:ss");

//        cout << i <<endl;
//        cout << s.name.toLocal8Bit().data() <<endl;
    }

//    cout << length << endl;
//    cout << res->length()<< endl;

//    cout << res[0].toLocal8Bit().data() <<endl;
//    cout << res[1].toLocal8Bit().data() <<endl;
//    cout << res[2].toLocal8Bit().data() <<endl;
//    cout << res[3].toLocal8Bit().data() <<endl;
//    cout << res[4].toLocal8Bit().data() <<endl;
//    cout << res[5].toLocal8Bit().data() <<endl;
//    cout << res[6].toLocal8Bit().data() <<endl;
//    cout << res[7].toLocal8Bit().data() <<endl;

    return res;

}

void TrainManagement::clearTrain()
{
    train = Train();
}

QString TrainManagement::getTrainNum()
{
    if(train.ID == NULL)
        return "";
    return train.ID;
}
